<template>
	<view class="header">
		<view class="shop">
			<image :src="query.category_image" mode="aspectFill" class="Boutique"></image>
			<view class="title">
				{{query.shop_name}}
			</view>
			<view class="right" @click="toggle">
				<view class="size">
					切换
				</view>
				<image :src="$IMG_URL('/static/home/indicate.png')" mode="" class="icon"></image>
			</view>
		</view>
		<view class="row">
			<toggleButton @toggleSwitch="changeIndex" @changeOpen="changeOpen" :isActive="isActiveIndex"
				:computedDay="day" :open="openShow"></toggleButton>
		</view>
		<u-picker mode="time" v-model="openShow" :params="params" :customTimeTag="true" :title="pickerTitle"
			:popupheight="'786rpx'" :borderRadius="16" @confirm="dateConfirm"></u-picker>
	</view>
</template>

<script>
	import toggleButton from './toggle-button.vue'
	export default {
		components: {
			toggleButton
		},
		props: {
			query:{
				type: Object,
				default: {},
				required: true
			}
		},
		data() {
			return {
				isActiveIndex: 0,
				day: '',
				openShow: false,
				// picker
				pickerTitle: '',
				params: {
					year: false,
					month: false,
					day: false,
					hour: false,
					minute: false,
					second: false
				},
			}
		},
		created() {
			this.day = this.formatDate(this.isActiveIndex)
		},
		methods: {
			toggle() {
				this.$emit('toggle')
			},
			changeIndex(e) {
				console.log('changeIndexe', e);
				this.isActiveIndex = e
				this.day = this.formatDate(e)
				this.$emit('changeIndex', e)
			},
			formatDate(index) {
				const today = new Date();
				const year = today.getFullYear();
				const month = String(today.getMonth() + 1).padStart(2, '0');
				const day = String(today.getDate()).padStart(2, '0');
				return index === 0 ? `${year}-${month}-${day}` : index === 1 ? `${year}-${month}` : `${year}`
			},
			changeOpen() {
				this.openShow = !this.openShow
				if (this.openShow) {
					this.pickerTitle = this.isActiveIndex == 0 ? '选择日期' : this.isActiveIndex == 1 ? '选择月份' : '选择年份'
					let params = {
						year: false,
						month: false,
						day: false,
						hour: false,
						minute: false,
						second: false
					};

					// 根据isActiveIndex设置params的值
					// 简写条件设置
					params.year = this.isActiveIndex < 3;
					params.month = this.isActiveIndex < 2;
					params.day = this.isActiveIndex < 1;
					this.params = params
				}
			},
			dateConfirm(e){
				console.log('e', e , this.$PICKERDATA(e));
				this.day = this.$PICKERDATA(e)
				this.$emit('dateConfirm', e , this.$PICKERDATA(e))
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header_bottom{
		width: 100%;
		background: #ffffff;
	}
	.header {
		width: 750rpx;
		height: 184rpx;
		background: #ffffff;
		padding: 26rpx 34rpx 0;

		.shop {
			width: 100%;
			display: flex;
			flex-direction: row;
			align-items: center;
			margin-bottom: 22rpx;

			.Boutique {
				width: 114rpx;
				height: 36rpx;
				margin-right: 12rpx;
			}

			.title {
				flex: 1;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC-Bold;
				font-weight: 700;
				text-align: left;
				color: #333333;
			}

			.right {
				display: flex;
				flex-direction: row;
				align-items: center;

				.size {
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC-Regular;
					font-weight: 400;
					color: #666666;
					margin-right: 8rpx;
				}

				.icon {
					width: 10rpx;
					height: 16rpx;
				}
			}
		}

		.row {
			width: 100%;
			display: flex;
			flex-direction: row;
			align-items: center;
		}
	}
</style>